package com.xindaoapp.happypet.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.xindaoapp.happypet.bean.PhotoAibum;
import com.xindaoapp.happypet.bean.PhotoItem;

/**
 * 本地图片管理类
 * 
 * @author chenghao
 * 
 */
public class PhotoManager {
    // 设置获取图片的字段信息
    private static final String[] STORE_IMAGES = {MediaStore.Images.Media.DISPLAY_NAME, // 显示的名�?
            MediaStore.Images.Media.LATITUDE, // 维度
            MediaStore.Images.Media.LONGITUDE, // 经度
            MediaStore.Images.Media._ID, // id
            MediaStore.Images.Media.BUCKET_ID, // dir id 目录
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME // dir name 目录名字
            };

    private final Context mContext;

    public PhotoManager(Context context) {
        super();
        mContext = context;
    }

    /**
     * 按相册获取图片信息
     */
    public List<PhotoAibum> getPhotoAlbum() {
        List<PhotoAibum> aibumList = new ArrayList<PhotoAibum>();
        Cursor cursor =
                MediaStore.Images.Media.query(mContext.getContentResolver(),
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);
        Map<String, PhotoAibum> countMap = new HashMap<String, PhotoAibum>();
        PhotoAibum pa = null;
        ArrayList<PhotoItem> allPhotos = new ArrayList<PhotoItem>();
        String firstItemInfo = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String dir_id =
                        cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID));
                PhotoItem info = null;
                if (!countMap.containsKey(dir_id)) {
                    pa = new PhotoAibum();// MediaStore.Images.Media.BUCKET_DISPLAY_NAME
                    pa.name =
                            cursor.getString(cursor
                                    .getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
                    pa.imageFilePath =
                            cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    pa.count = 1;
                    if (TextUtils.isEmpty(firstItemInfo)) {
                        firstItemInfo = pa.imageFilePath;
                    }
                    countMap.put(dir_id, pa);
                } else {
                    pa = countMap.get(dir_id);
                    pa.count = pa.count + 1;
                }
                pa.bitList.add(info =
                        new PhotoItem(false, cursor.getString(cursor
                                .getColumnIndex(MediaStore.Images.Media.DATA))));
                allPhotos.add(info);
            }
            cursor.close();
        }

        aibumList.add(new PhotoAibum("所有图片", -1, firstItemInfo, allPhotos));
        Iterable<String> it = countMap.keySet();
        for (String key : it) {
            aibumList.add(countMap.get(key));
        }

        return aibumList;
    }
}
